How to load script conditionally on custom field in wp_postmeta?

EDIT:
If you use $post in a function, you have to define it of course:

function xyz(){
    global $post;
    //Your code
}

Initial answer, might help others:
It appears to me, you do not use it inside an action or if so, you use it to early. It should work inside the ‘init’ action. So you could do something like

<?php
add_action( 'init', 'enqueue_conditionally' );
function enqueue_conditionally(){
    $mathjax = get_post_meta( get_the_ID(),'mathjax',true);
    if($mathjax == 'y'){
        wp_enqueue_script('mathjax');
    }
}
?>

Ref.: https://codex.wordpress.org/Plugin_API/Action_Reference/init