I have the same issue, rattling head liner. Like you, another owner used a type of foam, wedging it between the plastic bracing just at the back of the moon roof. Seems to have stopped the noise 100% without dealer repairs.
Looks like I have to try this today. Since I have to make a trip to Home Depot, I need to buy some foam and fix this.
2012 Focus Titanium - 18" wheels with handling package
2009 GT500 Shelby (700 rwhp) - sold (tired of feeding the car's thirst for fuel and my hunger for more power)