How can I secure a metal frame to polyurethane foam?
The best way is to embed (bent edges of) the wire mesh in the foam while the foam is wet, and let the foam harden around it. Since it's now too late for that, try large coarse screws, or bend a wire into a section of a circle and poke it into the surface, in a manner where the bent-circle aspect means it pops back out of the foam face elsewhere. Such tie wires could also be better done as embeds while the foam is wet, with a rod, nail or washer wrapped by the wire inside the foam to give them a better hold.

On the screw front, screw-in drywall anchors might give a better grab to your screws, since even the coarsest screw hasn't got much hold in foam.