Whatever you use, it has to deal with relative motion, and most insulating foams for houses are going to break apart or fail in shear because they don't have enough elongation. Once the foam fails, it will either rattle or it will squeak, and then you will set the car on fire to free yourself from it.
There are some flexible foams for automotive use, but they're more for cavity stiffening and anti-vibration.
You could use an automotive seam sealer or a urethane adhesive sealant; there are a few types and I would likely choose one of the thicker ones that can be used on vertical surfaces (it won't try to leak everywhere before it cures). Some of the urethane stuff (3M, etc) can be bought in caulk tubes for sealing RV and trailer bodies during manufacturing.
You can use a mesh tape to bridge bigger gaps and support the seam sealer like a diaphragm, or you could make a temporary dam for the sealer or use a backer rod or whatever.
You need something elastic though that can deal with chassis flex. How big the gap is, and how much relative motion there is are the driving factors here.
You could maybe use the 2-piece aftermarket firewall grommets with the chrome flanges, but they won't be very waterproof, relatively speaking. More draft sealing and noise blocking, like on a firewall (obviously).
There are also some edge seals that can install onto panel edges, kind of like the firewall grommet but sold by-the-foot. Basically a type of weatherstripping.
Just tossing out some ideas.