Yes. Spray foam insulation can be applied to decking underneath metal roofing panels prior to the installation of the metal panels. Similar to other types of insulation, spray foam on the deck should not create any issues with metal roofing. See your insulation representation for best application practice.
The Spray Polyurethane Foam Alliance, an association representing leaders in the SPF industry, has created a guideline for insulating metal buildings with spray polyurethane foam. It can be reviewed by clicking here. More information is available on their website at www.sprayfoam.org.