A 2016 flagship is better than a 2017 mid-range phone. Heck, even a 2015 flagship can outperform the mid-rangers of today. I know newer phones will have the latest software, but still.
Yeah this is absolutely the most common question we get asked these days, because a) mid-range phones are getting better, and b) flagships are aging better.
But the thing to think about is how important software and, perhaps more importantly, security updates, are to your wellbeing.
Let's compare something like the Galaxy S6, for instance, which can be found for around $300 new right now, to something like the Moto Z Play. The Galaxy S6 is technically much faster than the Moto Z Play, but is still waiting on a Nougat update, and doesn't regularly receive security updates. It's also likely to be abandoned sooner than the Moto Z Play.
The other thing is chip support: the Exynos 7420 in the Galaxy S6 is likely not going to receive support for as long as the Snpadragon 625 in the Z Play, which could potentially limit its ability to receive updates in the future.
I'm not necessarily recommending the mid-ranger over the older flagship, but you have to be aware that update cycles are business cycles, and older devices are abandoned sooner these days, regardless of where the device lives in the pecking order.