How to add google_tag script to header with custom environment variables?
The recommendation for adding javascript is to use an external file


That is not always the case and it is more of a guideline than a hard rule. 

There is a way to do what you ask, but you shouldn't because the google_tag module does exactly what you to want to do. It also allows you to include the GTM snippet inline or as a file.