Display custom field on 404 page outside loop

You should not hide functionality in a template file. Templates are dumb, you don’t run unit tests on them normally.

I’d recommend to move the logic to a separate function in your functions.php:

function header_class( $default="white" )
{
    $post = get_post();

    // 404 pages, empty archives (user, taxonomy, date)
    if ( empty( $post ) )
        return $default;

    $header = get_field( 'header', $post->ID );

    if ( empty( $header ) || ! is_scalar( $header ) )
        return $default;

    return esc_attr( $header );
}

And in your template you can now just call the function:

<header id="masthead" class="site-header <?= header_class() ?>" role="banner">