Add script to page at certain location in wordpress

To answer your first question, you can always add a <script> tag inside your template files wherever you like, would that solve it? Or alternatively add a function <?php add_my_script(); ?> somewhere in your template files which you put in functions.php and does some more checks to determine if you want a script there on that location and add it.

However, do you really need it at a certain location in your page? It should not make a difference where you put the script, because you can wrap your functionality inside a function, and only run it when you call the function.

To answer your second question, yes you can, it’s been answered before.
https://stackoverflow.com/questions/3943198/parse-a-javascript-file-through-php

So don’t call a .js file, call a .php file that renders your script.
just make sure the to use a header("Content-Type: application/javascript"); and whatever logic you want there