How to return a string that has a jQuery and Ajax inside in a shortcode?

For large amounts of HTML you’re better off using output buffering with ob_start() and ob_get_clean(). The documentation for it is here, but the short version is that it lets you capture output to return later. That way you can just output HTML like you would in a template, but capture it and return it at the end:

function wpse_279827_shortcode() {
    // Do PHP stuff.
    $message="Hello world!";
    // Start capturing output.
    ob_start(); 
    // Close PHP tags and start outputting HTML.
    ?>

    <div>
        <h1>HTML can go here</h1>
        <p>And also php code:</p>
        <p><?php echo 'Hello!'; ?></p>
    </div>
    <script>
        jQuery(document).ready(function() {
            alert('Message variable from PHP: <?php echo $message; ?>');
        });
    </script>

    <?php
    // Reopen PHP tags and return captured output.
    return ob_get_clean(); 
}
add_shortcode( 'wpse_279827_shortcode', 'wpse_279827_shortcode' );