The brain location is the result of evolution, but if we talk in simple engineering terms:
The brain is located in the head because the eyes are a part of the brain, and the eyes need to be quickly movable in order to take in the world around the creature. Another reason is that the eyes need to be close to the brain to minimize signal lag between the eyes and the parts of the brain that analyses the signals. A third reason is to minimize the cost of building the connection between the brain and the eyes, and keep the connection running.
Other senses like smell and hearing are also closely related to the brain, and also go together with the location of the brain.
From an evolutionary point of view:
Sight is one of the oldest sense organs. Sight coevolved with a control and signal analysis center, the brain, and set the body plan for all creatures that followed. They all have the layout with a head with a brain and eyes, or similar sense organs, in one end of a tube where food goes in, and the other end of a tube where the excrement goes out.
This flatworm is similar to what our ancestor 750 million years looked like. It gave rise to both humans and octopi.