Decompose the dependent variable as fitted values and residuals, $$ y=\hat y+e=X\hat\beta+e $$ If the regression is such that residuals sum to zero (typically because it contains a constant), we have, with $i$ a vector of ones, $$ \frac{i'y}{n}=\frac{i'X}{n}\hat\beta+\frac{i'e}{n}==\frac{i'X}{n}\hat\beta $$$$ \frac{i'y}{n}=\frac{i'X}{n}\hat\beta+\frac{i'e}{n}=\frac{i'X}{n}\hat\beta $$ or $$ \bar y=\bar X\hat\beta $$