Two relevant economic principles are:
Economies of scale: These are a more significant issue for middle and especially high schools which have a greater need for specialist teachers, as others have pointed out, and also for specialist facilities such as science labs with supporting technicians. Such teachers and facilities would tend to be under-utilised in small schools, resulting in higher costs of provision per child. Elementary schools, on the other hand, rely more on generalist teachers so can operate efficiently on a smaller scale.
Travel costs: Older children are more likely to make their own way to school, while younger children are more likely to be accompanied by a parent. Hence the costs of travel to school, for a given journey, and including not only fares and/or fuel costs (if any) but also and especially the opportunity cost of a parent's time, tend to be greater for younger children. While these costs do not impact directly on a school's budget, they are likely (for state schools) to add to political pressure to provide or keep open smaller elementary schools serving a small neighbourhood.