Understanding the Generalization Benefit of Model Invariance from a Data Perspective