expand treats the file as Groovy template. So you for example have to escape backslashes and escape dollars if they should be literal. On the other hand you can also do calculations in that file that are evaluated during the expand ${new Date()}.
The expansion mechanism also is not aware of syntax rules or that it is a Groovy for, so you cannot ignore comments as they are not comments, they are just text like the whole file.
If you don’t want the Groovy template behavior, you must not use expand but one of the other replacement features that does what you want.