Is there a better way to output HTML as a shortcode?

Shortcodes should always return data rather than echoing it. That way it appears in the right place on the page.

A couple of ways you can do this:

Option 1 – add to variable

function my_output() {
    $output="<div>" . $myvariable . '</div>';
    return $output;
}

Option 2 – use the object buffer

function my_output() {
    // Start the object buffer, which saves output instead of outputting it.
    ob_start();
    ?>
    <div>
    <?php echo $myvariable; ?>
    </div>
    <?php
    // Return everything in the object buffer.
    return ob_get_clean();
}