If you’re already using create-react-app, this is already available by updating to
<script src="https://maps.googleapis.com/maps/api/js?key=%REACT_APP_API_KEY%"></script>
as it says in the docs
You can also access the environment variables starting with REACT_APP_ in the public/index.html.