Hi @Pcanimal,
An approach could be to create a list in SharePoint with one item. You could store the current reference number in there and update it when needed. A flow could be trigger when there is no match for a reference number in the subject of a newly received e-mail. You could use a trigger condition expression with an indexOf function for that.
Below is an example of that approach.
1. Create a list with one item.

2. Create a flow with a when a new email arrives trigger action. In my example I used a shared mailbox. In the settings add a trigger condition expression like below:
@equals(indexOf(triggerOutputs()?['body/subject'], 'Ref:'), -1)
This expression checks if it can find the text 'Ref:' in the subject of the received e-mail. If it cannot find it (result would be -1) it would trigger the flow.

3. In the rest of the flow you could use the Get Item and Update Item action. In this example I am also sending an auto-reply with the reference number to the person e-mailing.
This expression is used to convert the title value in the list from text to a number, increment it with one and format it back to you desired format (in this case D4).
formatnumber(add(int(outputs('Get_item')?['body/Title']), 1), 'D4')
