yes, you can do that. You would have to design the Nextion HMI to have a text field on screen, then via serial from the ESP8266 (D1 mini, etc.) you would send the proper command to the Nextion display with the text as the payload to be displayed.
Check out the Home Assistant Switch Plate (HASP) Project. While it doesn’t use ESPHome, the concepts are the same.