It was a coordinated move. Iran notified the Americans in advanced about the attack and the US conveyed to Iran what scale and targets would be acceptable to them.
Both sides leverage the attack to prevent the liberation of Rafah. The Americans leverage this to exert more control over Israel while at the same time making them appear like they are protecting Israel.