What is the “proper” way to generate a javascript variable depending on a custom field value?

You may wish to look at this question. In that question, the admin-ajax url is passed through, but it can be any variable (or array of variables), like for instance $nameAttribute.

For instance, you would call

wp_localize_script('my_js_file_handle','my_js_object',array('attribute'=>$nameAttribute))` 

then in your js file, the variable would be accessible from my_js_object.attribute.

(You must enqueue the javascript file though!)