Yes, that's what's called net metering: https://en.wikipedia.org/wiki/Net_metering
However, despite being convenient for the user, it isn't great for the grid nor scalable if all users were to have solar panels. (Here in Italy solar on rooftops are quite widespread).
You can see that a kWh on a sunny summer noon when all panels the area are working well, isn't worth the same as a kWh in the cold winter night, where non-renewables often supply the grid.